package com.plm.frame.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.plm.frame.service.CartService;
import com.plm.frame.service.DingdanService;
import com.plm.frame.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.plm.frame.core.Constants;
import com.plm.frame.model.Cart;
import com.plm.frame.model.Dingdan;
import com.plm.frame.model.Product;
import com.plm.frame.model.User;

@Controller
public class DingdanAction extends BaseAction{

	@Resource
	private DingdanService dingdanService;
	@Resource
	private ProductService productService;
	@Resource
	private CartService cartService;
	
	@RequestMapping("/addDingdan")
	public String addDingdan(Map<String,List> m,Product product,Integer count){
		Dingdan dingdan = new Dingdan();
		dingdan.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		dingdan.setProduct(productService.selectProductAsId(product));
		dingdan.setCount(count);
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		dingdan.setDate(sdf.format(d));
		System.out.println(dingdan.toString());
		dingdanService.addDingdan(dingdan);
		m.put("dingdan", dingdanService.selectDingdan(dingdan));
		return "redirect:dingdan";
	}
	//购物车到订单直接查询按照用户名将所有购物车添加到订单
	@RequestMapping("/cartToDingdan")
	public String cartToDingdan(Map<String,List> m){
		Cart cart = new Cart();
		cart.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		List<Cart> a = cartService.selectCart(cart);
		Dingdan dingdan = new Dingdan();
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		for (int i = 0; i < a.size(); i++) {		
			dingdan.setCart(a.get(i));
			dingdan.setDate(sdf.format(d));
			dingdanService.addDingdan(dingdan);
			cartService.deleteCart(a.get(i));
		}
		m.put("dingdan", dingdanService.selectDingdan(dingdan));
		return "redirect:dingdan";
	}
	
	@RequestMapping("/dingdan")
	public String dingdan(Map<String,Object> m){
		Dingdan dingdan = new Dingdan();
		dingdan.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		m.put("dingdan", dingdanService.selectDingdan(dingdan));
		List<Dingdan> dingdans = dingdanService.selectDingdan(dingdan);
		if( dingdans.size() == 0){
			m.put("wudingdan","订单为空");
		}
		return "dingdan";
	}
	
	@RequestMapping("/dingdanVerification")
	public String dingdanVerification(Map m,Dingdan dingdan){
		//赋值订单并储存
		dingdan.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		Product product = new Product();
		product.setId(dingdan.getDid());
		dingdan.setProduct(productService.selectProductAsId(product));
		dingdanService.addDingdan(dingdan);
		return "redirect:showhotel?id="+product.getId();
	}
	
	@RequestMapping("/deleteDingdan")
	public String deleteDingdan(Map<String,List> m,Dingdan dingdan){
		dingdanService.deleteDingdan(dingdan);
		return "redirect:dingdan";
	}
	
	@RequestMapping("/admindeleteDingdan")
	public String admindeleteDingdan(Map<String,List> m,Dingdan dingdan){
		dingdanService.deleteDingdan(dingdan);
		return "redirect:admindingdan";
	}
}
